package Model.QBE;

import Model.SQL.*;
import java.util.*;
import java.sql.*;
import Model.Beans.*;
import Model.Beans.Results.*;

public class ScheduleModel extends DataAccessHelperBase
{
    public ScheduleModel() 
    {
        this.tableName = "schedule";
        super.customize(tableName);
    }
    
    public int insert(Schedule schedule)
    {
        String[] values = new String[2];
        values[0] = Integer.toString(schedule.getStudentId());
        values[1] = Integer.toString(schedule.getCourseId());
        return this.insertRecord(values);
    }
    
    public int delete(String student_id, String course_id)
    {
        return this.deleteRecord("_student = " + student_id + " AND _course = " + course_id);
    }
    
    public int delete(Schedule schedule)
    {
        return this.delete(Integer.toString(schedule.getStudentId()), Integer.toString(schedule.getCourseId()));
    }
    
    
    public int modify(String student_id, String course_id, String new_student_id, String new_course_id)
    {   
        String id_str = "_student = " + student_id + " AND _course = " + course_id; 
        String[] values = new String[2];
        
        int i = 0;
        if(new_student_id.length() > 0)
            values[i++] = "_student = '" + new_student_id + "'";
        if(new_course_id.length() > 0)
            values[i++] = "_course = '" + new_course_id + "'";
        return this.updateRecord(id_str, values);   
    }
    
    public int modify(Schedule schedule, Schedule newSchedule)
    {
        return this.modify(Integer.toString(schedule.getStudentId()), Integer.toString(schedule.getCourseId()), 
                Integer.toString(newSchedule.getStudentId()), Integer.toString(newSchedule.getCourseId()));
    }
    
    public ScheduleList search(String student_id, String course_id)
    {   
        String where = "";
        ResultSet rs = null;
        ScheduleList list = new ScheduleList();
        
        if(student_id.length() > 0 && !student_id.equals("0"))
            where += "_student = " + student_id + " AND ";
        if(course_id.length() > 0 && !course_id.equals("0"))
            where += "_course = " + "'" + course_id + "'";
        else if(where.length() > 0)
            where = where.substring(0, where.length()-4);
        rs = this.getData(where);
        
        if(rs == null)
            return list;
        try 
        {
            while(rs.next()) 
            {
                int _student  = rs.getInt("_student");
                int _course = rs.getInt("_course");

                Schedule tmp = new Schedule();
                tmp.setCourseId(_course);
                tmp.setStudentId(_student);
                
                list.addRelation(tmp);
                
                System.out.println(_student + "   " + _course);
            }
            return list;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: " + e.getMessage());
        }   
        finally
        {
            if (rs != null) 
            {
                try 
                {
                    rs.close();
                } catch (SQLException sqlEx) { }
                rs = null;
            }
        }
        return list;
    }
    
    public ScheduleList search(Schedule schedule)
    {
        return this.search(Integer.toString(schedule.getStudentId()), Integer.toString(schedule.getCourseId()));
    }
    
    /*
    public static void main(String[] args)
    {
        ScheduleModel smodel = new ScheduleModel();
        Schedule schedule = new Schedule();
        schedule.setCourseId(1);
        schedule.setStudentId(2);
        
        Schedule schedule2 = new Schedule();
        schedule2.setCourseId(3);
        schedule2.setStudentId(1);
        
        //smodel.delete(schedule2);
        Schedule tmp = new Schedule();
       // tmp.setCourseId(1);
        tmp.setStudentId(2);
        smodel.search(tmp);
    }
     */
}
